<script setup>
import { h, onMounted, ref, resolveDirective, withDirectives } from 'vue'
import {
  NAlert,
  NButton,
  NForm,
  NFormItem,
  NInput,
  NInputNumber,
  NPopconfirm,
  NSelect,
  NSwitch,
  NTag,
  NTreeSelect
} from 'naive-ui'

import CommonPage from '@/components/page/CommonPage.vue'
import QueryBarItem from '@/components/query-bar/QueryBarItem.vue'
import CrudModal from '@/components/table/CrudModal.vue'
import CrudTable from '@/components/table/CrudTable.vue'
import TheIcon from '@/components/icon/TheIcon.vue'

import { renderIcon } from '@/utils'
import { useCRUD } from '@/composables'
// import { loginTypeMap, loginTypeOptions } from '@/constant/data'
import api from '@/api'

defineOptions({ name: '问答信息' })

const $table = ref(null)
const queryItems = ref({})
const vPermission = resolveDirective('permission')

const {
  modalVisible,
  modalTitle,
  modalLoading,
  handleSave,
  modalForm,
  modalFormRef,
  handleEdit,
  handleDelete,
  handleAdd,
} = useCRUD({
  name: '问答信息',
  initForm: { order: 0 },
  doCreate: api.createAnswers,
  doUpdate: api.updateAnswers,
  doDelete: api.deleteAnswers,
  refresh: () => $table.value?.handleSearch(),
})

const isDisabled = ref(false)

const data = reactive({
  c_type:[],
  c_status:[],
})

onMounted(() => {
  $table.value?.handleSearch()
})

const deptRules = {
  question: [
    {
      required: true,
      message: '请输入问题',
      trigger: ['input', 'blur', 'change'],
    },
  ]
}

async function addDepts() {
  isDisabled.value = false
  handleAdd()
}

const columns = [
    {
    title: '问题',
    key: 'question',
    width: 200,
    align: 'center',
  },
    {
    title: '答案',
    key: 'answer',
    width: "auto",
    align: 'center',
    render(row) {
      return h(
          NAlert,
          { type: 'success', size: 'small', bordered: false, style: { margin: '2px 3px' } },
          { default: () => row.answer }
      )
    },
  },
  {
    title: '操作',
    key: 'actions',
    width: 100,
    align: 'center',

    render(row) {
      return [
        withDirectives(
            h(
                NButton,
                {
                  size: 'small',
                  type: 'primary',
                  style: 'margin-right: 8px;',
                  onClick: () => {
                    console.log('row', row.parent_id)
                    // if (row.parent_id === 0) {
                    //   isDisabled.value = true
                    // } else {
                    //   isDisabled.value = false
                    // }
                    handleEdit(row)
                  },
                },
                {
                  default: () => '编辑',
                  icon: renderIcon('material-symbols:edit', { size: 16 }),
                }
            ),
            [[vPermission, 'post/api/content/category/update']]
        ),
      ]
    },
  },
]

// 修改用户禁用状态
async function handleUpdateDisable(row) {
  if (!row.id) return
  row.publishing = true
  row.state = row.state === false
  row.publishing = false
  try {
    await api.updateJobsRule(row)
    $message?.success(row.state ? '已启动' : '已停止')
    $table.value?.handleSearch()
  } catch (err) {
    // 有异常恢复原来的状态
    row.state = row.state === false
  } finally {
    row.publishing = false
  }
}
</script>

<template>
  <!-- 业务页面 -->
  <CommonPage show-footer title="问答列表">
    <template #action>
      <div>
        <NButton
            v-permission="'post/api/content/category/create'"
            class="float-right mr-15"
            type="primary"
            @click="addDepts"
        >
          <TheIcon icon="material-symbols:add" :size="18" class="mr-5" />新建问答
        </NButton>
      </div>
    </template>
    <!-- 表格 -->
    <CrudTable
        ref="$table"
        v-model:query-items="queryItems"
        :columns="columns"
        :get-data="api.getAnswers"
    >
      <template #queryBar>
        <QueryBarItem label="问题" :label-width="80">
          <NInput
              v-model:value="queryItems.question"
              clearable
              type="text"
              placeholder="请输入问题"
              @keypress.enter="$table?.handleSearch()"
          />
        </QueryBarItem>

      </template>
    </CrudTable>

    <!-- 新增/编辑 弹窗 -->
    <CrudModal
        v-model:visible="modalVisible"
        :title="modalTitle"
        :loading="modalLoading"
        @save="handleSave"
    >
      <NForm
          ref="modalFormRef"
          label-placement="left"
          label-align="left"
          :label-width="80"
          :model="modalForm"
          :rules="deptRules"
      >

        <NFormItem label="问题" path="question">
          <NInput v-model:value="modalForm.question" clearable placeholder="请输入问题" />
        </NFormItem>
        <NFormItem label="答案" path="answer">
          <NInput  type="textarea" v-model:value="modalForm.answer" clearable
                        placeholder="答案"/>
        </NFormItem>


      </NForm>
    </CrudModal>
  </CommonPage>
</template>
